﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Northwind.NET.EDM.Reports;

namespace Northwind.NET.EDM.FE.WinForms.Controls
{
    public partial class ApplicationControlsManagerUserControl : UserControl
    {
        public ApplicationControlsManagerUserControl()
        {
            InitializeComponent();
        }

        #region Base
        protected string _formCaption;
        protected string _userControlName;
        protected string _entitySetName;
        public void Init(string formCaption, string userControlName, string entitySetName)
        {
            _formCaption = formCaption;
            _userControlName = userControlName;
            _entitySetName = entitySetName;
        }
        public void Init(string userControlName)
        {
            _userControlName = userControlName;
        }

        private BaseUserControl userControl;
        protected void loadControl()
        {
            loadControl(null);
        }

        public EventHandler<Northwind.NET.EDM.FE.WinForms.Controls.BaseUserControl.ControlLoadedEventArgs> ControlLoaded;  


        private UserControl _loadedControl;
        protected void loadControl(Panel controlPanel)
        {

            userControl = ControlsFactory.CreateControlInstance(_userControlName) as BaseUserControl;
            _formCaption = userControl.FormCaption;

            this.Text = _formCaption;

            if (userControl != null && userControl is BaseUserControl)
            {
                ((BaseUserControl)userControl).SetBindingSource();
            }

            this.SuspendLayout();

            userControl.Location = new System.Drawing.Point(13, 13);
            userControl.Name = string.Format("{0}1", _userControlName.Substring(0, 1).ToLower() + _userControlName.Substring(1));
            userControl.TabIndex = 0;

            if (controlPanel == null)
            {
                this.ClientSize = new System.Drawing.Size(userControl.Width + 20, userControl.Height + 20);
                this.Controls.Add(userControl);
            }
            else
            {
                _loadedControl = userControl;
                controlPanel.Controls.Add(userControl);

                userControl.Dock = DockStyle.Fill;
            }

            this.ResumeLayout(false);

            _lastLoadedControlName = _userControlName;
 
            if (ControlLoaded != null)
            {
                ControlLoaded 
                    (
                         this,
                         new Northwind.NET.EDM.FE.WinForms.Controls.BaseUserControl.ControlLoadedEventArgs()
                         {
                               ControlName = _userControlName,
                               ControlTitle = _formCaption 
                         }
                    );

                if (string.Compare(_userControlName, "ReportViewingUserControl", true) == 0)
                {
                    ReportViewingUserControl c = controlPanel.Controls[0] as ReportViewingUserControl;
                    if (c != null)
                    {
                        c.ControlLoaded += new EventHandler<BaseUserControl.ControlLoadedEventArgs>(reportViewerLoaded);
                    }
                }
            }
        }

        private void reportViewerLoaded(object sender, BaseUserControl.ControlLoadedEventArgs e)
        {
            if (ControlLoaded != null)
            {
                ControlLoaded
                    (
                         this,
                         new Northwind.NET.EDM.FE.WinForms.Controls.BaseUserControl.ControlLoadedEventArgs()
                         {
                             ControlName = e.ControlName,
                             ControlTitle = e.ControlTitle 
                         }
                    );
            }
        }

        private string _lastLoadedControlName;

        protected Panel _mainPanel;
        //protected void openChildFormInThisForm(Form thisForm, string childFormControlName)
        protected void openChildFormInThisForm(UserControl thisControl, string childFormControlName)
        {
            Init(childFormControlName);

            if (_loadedControl != null)
            {
                _mainPanel.Controls.Remove(_loadedControl);
            }

            loadControl(_mainPanel);
        }

        protected void todo()
        {
            informationMessage("TO DO!");
        }

        protected void informationMessage(string message)
        {
            MessageBox.Show(message, Northwind.NET.Settings.Globals.APPLICATION_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        #endregion

        private void UserControlManagerForm_Load(object sender, EventArgs e)
        {
            if (this.DesignMode) return;
            _mainPanel = this.mainPanel;

            //this.WindowState = FormWindowState.Maximized;

            cmdCategories_Click(sender, e);
        }

        public void ShowCategoriesForm()
        {
            cmdCategories_Click(this, new EventArgs());
        }

        private void cmdCategories_Click(object sender, EventArgs e)
        {
            openChildFormInThisForm(this, ControlsFactory.ControlNames.CategoriesUserControlName);
        }

        private void cmdSuppliers_Click(object sender, EventArgs e)
        {
            openChildFormInThisForm(this, ControlsFactory.ControlNames.SuppliersUserControlName);
        }

        private void cmdProducts_Click(object sender, EventArgs e)
        {
            openChildFormInThisForm(this, ControlsFactory.ControlNames.ProductsUserControlName);
        }

        private void cmdOrders_Click(object sender, EventArgs e)
        {
            openChildFormInThisForm(this, ControlsFactory.ControlNames.OrdersUserControlName);
        }

        private void cmdEmployees_Click(object sender, EventArgs e)
        {
            openChildFormInThisForm(this, ControlsFactory.ControlNames.EmployeesUserControlName);
        }
        private void showCustomersButton_Click(object sender, EventArgs e)
        {
            openChildFormInThisForm(this, ControlsFactory.ControlNames.CustomersUserControlName);
        }
        private void cusomerPhoneListButton_Click(object sender, EventArgs e)
        {
            openChildFormInThisForm(this, ControlsFactory.ControlNames.CustomersPhoneListUserControlName);
        }
        private void cmdCustomerOrders_Click(object sender, EventArgs e)
        {
            openChildFormInThisForm(this, ControlsFactory.ControlNames.CustomerOrdersUserControlName);
        }
        private void quaterlyOrdersButton_Click(object sender, EventArgs e)
        {
            openChildFormInThisForm(this, ControlsFactory.ControlNames.QuarterlyOrdersUserControlName);
        }

        private void cmdPrintSalesReports_Click(object sender, EventArgs e)
        {
            openChildFormInThisForm(this, ControlsFactory.ControlNames.ReportViewingUserControlName);
        }

        private void ApplicationControlsManagerUserControl_Load(object sender, EventArgs e)
        {
            if (this.DesignMode) return;
            _mainPanel = this.mainPanel;
       }
    }
}
